﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCSService;
using SCS.Core.Models;
using SCS.Core.ViewModels;
using MvcReCaptcha;
using Recaptcha;
using SCS.Core.Internal;


namespace SCS.Core.Controllers
{
    public class UserWebController :BaseFontendController
    {
        static string olUrl;

        public ActionResult SignIn()
        {
            IntPage();
            return View();
        }
              

        //[ChildActionOnly]
        //[HttpGet]
        //public ActionResult SignIns()
        //{
        //    var Login = new Models.CollectionFontend.LoginCollection();

        //    return PartialView("../BaseFontend/" + GetSiteGuid() + "/SignIns",Login);
        //}

        //[ChildActionOnly]
        //[HttpPost]
        //public ActionResult SignIns(FormCollection values)
        //{
        //    string olUrl = Request.UrlReferrer.AbsoluteUri;
        //    string redirectStr = "";
        //    var Login = new Models.CollectionFontend.LoginCollection();
        //    TryUpdateModel(Login);

        //    try
        //    {
        //            int idLogin = Models.ModelsFontend.UserWebModel.LoginUser(Login.UserName, Login.Password) != null ? Models.ModelsFontend.UserWebModel.LoginUser(Login.UserName, Login.Password).UserID : 0;
        //            if (idLogin > 0)
        //            {
        //                if (Login.Remember)
        //                {
        //                    HttpCookie ck = new HttpCookie("Sercurity_Uesr_Name");
        //                    ck.Value = Login.UserName;
        //                    ck.Expires = DateTime.Now.AddMonths(1);//Thoi han luu cookie
        //                    HttpContext.Response.Cookies.Add(ck);

        //                    HttpCookie ckPass = new HttpCookie("Sercurity_Uesr_Password");
        //                    ck.Value = Login.Password;
        //                    ck.Expires = DateTime.Now.AddMonths(1);//Thoi han luu cookie
        //                    HttpContext.Response.Cookies.Add(ckPass);
        //                }

        //                //Check website login for user
        //                List<System_Website> listSite = Models.ModelsFontend.UserWebModel.GetListWebsiteByUserCustomer(idLogin);

        //                if ((listSite.Count() == 0))
        //                {

        //                }
        //                else
        //                {
        //                    //int count = 0;
        //                    bool check = false;
        //                    foreach (var item in listSite)
        //                    {
        //                        if (DefaultHostGetter().Contains("localhost"))
        //                        {
        //                            if (item.SiteGuid == DefaultSiteGuidGetter())
        //                            {
        //                                check = true;
        //                            }
        //                        }
        //                        else
        //                        {
        //                            if (item.domain == DefaultHostGetter())
        //                            {
        //                                check = true;
        //                            }
        //                        }

        //                    }
        //                    if (check)
        //                    {
        //                        Session["UserID"] = idLogin;
        //                        Session["LogInSite"] = DefaultSiteGuidGetter();     //Ghi lai website khach hang da dang nhap
        //                        //Doi voi User co Permission cao nhat la 3 thi chi duoc login tren site da dang ky

        //                        //Ghi lại log SignIn
        //                        SCSService.Security_User_Log log = new Security_User_Log();
        //                        log.UserID = idLogin;
        //                        log.LoginDate = DateTime.Now;
        //                        log.LogDetail = "SignIn website :" + DefaultSiteGuidGetter();

        //                        Session["LogID"] = Models.ModelsFontend.UserWebModel.CreateLog(log);

        //                        if (DefaultHostGetter().Contains("localhost"))
        //                        {
        //                            if (Session["NextAction"] != null)
        //                            {
        //                                redirectStr=Session["NextAction"].ToString();
        //                            }
        //                            else
        //                                redirectStr="/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/Home/Index";
                                    
        //                            //return Redirect(Session["NextAction"] != null ? Session["NextAction"].ToString() : "/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/Home/Index");
        //                        }
        //                        else
        //                        {
        //                            if (Session["NextAction"] != null)
        //                            {
        //                                redirectStr=Session["NextAction"].ToString();
        //                            }
        //                            else
        //                                redirectStr="/" + DefaultLanguageGetter() + "/Home/Index";
        //                            //return Redirect(Session["NextAction"] != null ? Session["NextAction"].ToString() : "/" + DefaultLanguageGetter() + "/Home/Index");
        //                        }

        //                    }

        //                }
        //            }
        //            else
        //            {
        //                Session["UserID"] = null;
        //                Session["LogInSite"] = null;
        //            }
                        
        //    }
        //    catch
        //    {
        //        return PartialView("../BaseFontend/" + GetSiteGuid() + "/SignIns",Login); 
        //    }

        //    ViewData["redirectStr"] = redirectStr;
        //    return PartialView("../Commont/ResultSignIn");
        //}

        public JsonResult DeletePostArticleByPostID(string ids)
        {
            var Result = ids;
            PostArticleModel.DeletePost(int.Parse(Result));
            

            return this.Json(Result);
        }
        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult ForgotPassword()
        {
            IntPage();
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ForgotPasswords()
        {
            var forgot = new Models.CollectionFontend.ForgotCollection();

            return PartialView("../BaseFontend/" + GetSiteGuid() + "/ForgotPasswords", forgot);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ForgotPasswords(FormCollection collection)
        {
            var forgot = new Models.CollectionFontend.ForgotCollection();
            TryUpdateModel(forgot);
            try
            {
                int userForgot = Models.ModelsFontend.UserWebModel.UserForgotPassword(forgot.UserName, forgot.Email) != null ? Models.ModelsFontend.UserWebModel.UserForgotPassword(forgot.UserName, forgot.Email).UserID : 0;
                if (userForgot > 0)
                {
                        BaseService baseSvc = new BaseService();
                        string newPassword = baseSvc.RandomString(6, true);
                        SCSService.Security_User user = Models.ModelsFontend.UserWebModel.UserForgotPassword(forgot.UserName, forgot.Email);
                        user.Password =newPassword;
                        baseSvc.SendEmailForgotPassword(user.Email, newPassword);

                        Models.ModelsFontend.UserWebModel.ChangePassword(user);
                }

            }
            catch
            {
                return View(forgot);
            }
            return PartialView("../BaseFontend/" + GetSiteGuid() + "/ForgotPasswords");
        }


        public ActionResult Register()
        {
            IntPage();
            return View();
        }

        public JsonResult CheckEmailCreated(string id)
        {
            bool re = false;
            int count = Models.ModelsFontend.UserWebModel.GetCountUserByEmail(id);

            if (count > 0)
            {
                re = false;
            }
            else
            {
                re = true;
            }

            return this.Json(re);
        }

        public JsonResult CheckSubdomainCreated(string id)
        {
            bool re = false;
            int count = Models.ModelsFontend.UserWebModel.GetCountWebsiteByDomain(id);

            if (count > 0)
            {
                re = false;
            }
            else
            {
                re = true;
            }

            return this.Json(re);
        }    

        [ChildActionOnly]
        [HttpGet]
        public ActionResult Registers()
        {
            var register = new Models.CollectionFontend.RegisterCollection();

            return PartialView("../BaseFontend/" + GetSiteGuid() + "/Registers", register);
        }

        [ChildActionOnly]
        //[CaptchaValidator]
        [HttpPost]
        public ActionResult Registers(FormCollection collection)//, bool captchaValid)
        {
            var register = new Models.CollectionFontend.RegisterCollection();
            TryUpdateModel(register);
            int cusID = 0;
            int userID = 0;
            try
            {

                //if (captchaValid)//success captcha
                //{
                if (Session["Captcha"] == null || Session["Captcha"].ToString() != register.Captcha)
                {
                    ModelState.AddModelError("Captcha", "Kết quả sai.Xin vui lòng thử lại.");
                    //dispay error and generate a new captcha
                    ViewData["Result"] = "Xin vui lòng nhập kết quả.";
                    return PartialView("../BaseFontend/" + GetSiteGuid() + "/Registers", register);
                }
                else
                {
                    if (register.Password.Equals(register.ConfirmPassword))
                    {
                        //Check UserName and Email is Unique
                        //if (Models.ModelsFontend.UserWebModel.GetCountUserByUserName(register.UserName) > 0)
                        //{
                        //    ViewData["Result"] = "UserName has been used!";
                        //    return View(register);
                        //}

                        if (Models.ModelsFontend.UserWebModel.GetCountUserByEmail(register.Email) > 0)
                        {
                            ViewData["Result"] = "Địa chỉ Email này đã được sử dụng.";
                            return PartialView("../BaseFontend/" + GetSiteGuid() + "/Registers", register);
                        }

                        if (!register.Agree)
                        {
                            ViewData["Result"] = "Xin vui lòng tích vào điều kiện đồng ý với 'Quy định sử dụng và chính sách bảo mật của chúng tôi'.";
                            return PartialView("../BaseFontend/" + GetSiteGuid() + "/Registers", register);
                        }

                        SCSService.BaseService baseSvc = new BaseService();

                        //Create User
                        SCSService.Security_User user = new Security_User();
                        user.UserName = register.Email;
                        user.Email = register.Email;
                        user.Password = baseSvc.EnMD5(register.Password);
                        user.CreateDate = DateTime.Now;
                        user.UserStatus = 1;
                        user.RandomText = baseSvc.RandomString(4, true);
                        user.RoleID = 1;                                                     //Khách hàng đã đăng ký
                        userID = Models.ModelsFontend.UserWebModel.CreateUser(user);

                        if (userID > 0)
                        {

                            //Create Customer
                            SCSService.Customer customer = new Customer();
                            customer.CustomerName = register.FullName;
                            customer.UserID = userID;
                            customer.Email = register.Email;
                            customer.CustomerCode = baseSvc.GetCodeRamdom();
                            customer.Status = 1;
                            customer.CreateDate = DateTime.Now;
                            customer.ActiveDate = DateTime.Now;
                            customer.Balance = 0;

                            cusID = Models.ModelsFontend.UserWebModel.CreateCustomer(customer);

                            int siteGuid = 0;
                            if (Request.Url.Host == "localhost")
                            {
                                if (Request.Url.ToString().Contains("favicon.ico"))
                                {

                                }
                                else
                                    siteGuid = DefaultSiteGuidGetter();
                            }
                            else
                                siteGuid = Models.ConfigModel.GetWebsitebyDomain(DefaultHostGetter()).SiteGuid;

                            //Create User Website
                            SCSService.Security_Website_User webUser = new Security_Website_User();
                            webUser.UserID = userID;
                            webUser.SiteGuid = siteGuid;
                            webUser.CreateDate = DateTime.Now;
                            Models.ModelsFontend.UserWebModel.CreateWebsiteUser(webUser);

                        }
                        //Register thanh cong tu chuyen ve trang chu va dang nhap cho user vua tao
                        Session["UserID"] = userID;
                        Session["LogInSite"] = DefaultSiteGuidGetter();
                        ViewData["Result"] = "Đăng ký thành công";


                    }
                    else
                    {
                        Session["UserID"] = null;
                        Session["LogInSite"] = null;
                        ViewData["Result"] = "Bạn hãy xác nhận lại mật khẩu";
                    }
                }
                //}
                //else
                //{
                //    ViewData["Result"] = "Code Invalid!";
                //    return PartialView("../BaseFontend/" + GetSiteGuid() + "/Registers", register);
                //}
                              
            }
            catch
            {
                return PartialView("../BaseFontend/" + GetSiteGuid() + "/Registers", register);
            }
            return PartialView("../BaseFontend/" + GetSiteGuid() + "/Registers");
        }

        
        
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

 
        //public ActionResult LogOut()
        //{
        //    olUrl = Request.UrlReferrer.AbsoluteUri;
        //    Session["UserID"] = null;
        //    Session["LogInSite"] = null;
        //    return Redirect(olUrl);
        //}
        [ChildActionOnly]
        public ActionResult ListPostArticle()
        {
            
            if (Session["UserID"] == null)
            {
                return Redirect("/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
            }
            int iLanguageID = StockModel.GetLanguageIDByCountryCode(DefaultLanguageGetter());
            int iPostUser = int.Parse(Session["UserID"].ToString());
            List<Post> IPostArticles = PostArticleModel.IGetListPostByIdCate(null, iPostUser, 24, iLanguageID);

            ListPostArticleViewModels lPostArticle = new ListPostArticleViewModels();

            if (IPostArticles != null)
            {
                lPostArticle.lPosts = IPostArticles;
                 return PartialView("../BaseFontend/" + GetSiteGuid() + "/ListPostArticle", lPostArticle);
       
            }

            return PartialView("../BaseFontend/" + GetSiteGuid() + "/ListPostArticle",new ListPostArticleViewModels());
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ListPostArticle(int? PostID)
        {

            PostArticleModel.DeletePost(PostID);
            return PartialView("../BaseFontend/" + GetSiteGuid() + "/ListpostWithAcc");
        }

        public ActionResult ChangePassword()
        {
            IntPage();
            if (Session["UserID"] == null)
            {
                if (DefaultHostGetter().Contains("localhost"))
                {
                    return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }
                else
                {
                    return Redirect("/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }
                
                
            }

            if (DefaultHostGetter().Contains("localhost"))
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }
            else
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }

               
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ChangePasswords()
        {
            var changePasswords = new Models.CollectionFontend.ChangePassword();

            return PartialView("../BaseFontend/" + GetSiteGuid() + "/ChangePasswords", changePasswords);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ChangePasswords(FormCollection values)
        {
            SCSService.BaseService baseSvc = new BaseService();
            var change = new Models.CollectionFontend.ChangePassword();
            TryUpdateModel(change);
            string error = "";
            try
            {

                
                if (Models.ModelsFontend.UserWebModel.GetUerByID(int.Parse(Session["UserID"].ToString())).Password.Equals(baseSvc.EnMD5(change.Password)))
                {
                    if (change.NewPassword.Equals(change.ConfirmPassword))
                    {
                        SCSService.Security_User user=Models.ModelsFontend.UserWebModel.GetUerByID(int.Parse(Session["UserID"].ToString()));
                        user.Password=change.NewPassword;
                        Models.ModelsFontend.UserWebModel.ChangePassword(user);
                        error = "Bạn đã đổi mật khẩu thành công.";
                    }
                    else
                        error = "sai mật khẩu!";
                }
                else
                {
                    error = "sai mật khẩu!";
                }
                ViewData["error"] = error;

                //if (error == "")
                //{
                //    return Redirect(olUrl);
                //}
            }
            catch
            {
                return PartialView("../BaseFontend/" + GetSiteGuid() + "/ChangePasswords", change);
            }
            ViewData["error"] = error;
            return PartialView("../BaseFontend/" + GetSiteGuid() + "/ChangePasswords");
        }

        [ChildActionOnly]
        public ActionResult MenuAccountInformation()
        {
            return PartialView("../BaseFontend/" + GetSiteGuid() + "/MenuAccountInformation");
        }
        public ActionResult ListpostWithAcc()
        {
            IntPage();
            if (Session["UserID"] == null)
            {
                if (DefaultHostGetter().Contains("localhost"))
                {
                    return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }
                else
                {
                    return Redirect("/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }


            }

            if (DefaultHostGetter().Contains("localhost"))
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }
            else
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }


            return View();
        }


        public ActionResult AccountInformation()
        {
            IntPage();
            if (Session["UserID"] == null)
            {
                if (DefaultHostGetter().Contains("localhost"))
                {
                    return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }
                else
                {
                    return Redirect("/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }


            }

            if (DefaultHostGetter().Contains("localhost"))
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }
            else
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }


            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ProfileUser(int id)
        {
            var us = Models.UserModel.GetUerByID(id);
            var user = new Models.Collection.UserCollection();
            var cus = new SCSService.Customer();

            user.UserName = us.UserName;
            user.Email = us.Email;
            user.CreateDate = us.CreateDate;
            user.LastModified = us.CreateDate;
            user.UserStatus = us.UserStatus;
            user.UserDes = us.UserDes;
            user.RoleID = us.RoleID;
            user.UserID = us.UserID;

            if (us.UserID != 0)
            {
                cus = Models.ModelsFontend.ShoppingCart.GetCustomerByUserID(us.UserID);
                if (cus != null)
                {
                    user.CustomerName = cus.CustomerName??"";
                }
                

            }

            ViewData["IsAdd"] = false;

            return PartialView("../BaseFontend/" + GetSiteGuid() + "/ProfileUser", user);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ProfileUser(FormCollection collection)
        {
            var user = new Models.Collection.UserCollection();
            TryUpdateModel(user);

            var us = Models.UserModel.GetUerByID(user.UserID ?? 0);
            if (us != null)
            {
                us.Email = user.Email;
                if (us.UserName == us.Email)
                {
                    us.UserName = user.Email;
                }

                Models.ModelsFontend.UserWebModel.UpdateUser(us);
                var cus = new SCSService.Customer();
                cus = Models.ModelsFontend.ShoppingCart.GetCustomerByUserID(us.UserID);
                cus.CustomerName = user.CustomerName;

                Models.ModelsFontend.UserWebModel.UpdateCustomer(cus);
            }

            return PartialView("../BaseFontend/" + GetSiteGuid() + "/ProfileUser", user);
        }

        public ActionResult HistoryTransaction()
        {
            IntPage();
            if (Session["UserID"] == null)
            {
                if (DefaultHostGetter().Contains("localhost"))
                {
                    return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }
                else
                {
                    return Redirect("/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }


            }

            if (DefaultHostGetter().Contains("localhost"))
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }
            else
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }


            return View();
        }


        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult HistoryOrder()
        {
            int userID = int.Parse(Session["UserID"].ToString());
            int currentPage = 1;
            int pageSize = 9;

            ViewData["currentPage"] = currentPage;
            ViewData["pageSize"] = pageSize;

            return HistoryOrder(null, null, null, currentPage, pageSize, null, userID);
        }

        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult HistoryOrder(int? status, DateTime? startDate, DateTime? endDate, int? currentPage, int? pageSize, string gridAction, int? id)
        {
            int LanguageID = StockModel.GetLanguageIDByCountryCode(DefaultLanguageGetter());
            int siteGuid = GetSiteGuid();


            //id đầu vào là userID
            if (gridAction == "PageSizeChanged")
                currentPage = 1;

            if (currentPage == null && gridAction == "CurrentPageChanged")
            {
                string strCurrentPage = Request.Form.Get("quickPage").Replace(",", "");
                currentPage = int.Parse(strCurrentPage);
            }
            else if (currentPage == null)
            {
                currentPage = 1;
            }

            if (pageSize == null)
            {
                pageSize = 9;
            }

            ViewData["currentPage"] = currentPage;

            var cus = new SCSService.Customer();
            cus = Models.ModelsFontend.ShoppingCart.GetCustomerByUserID(id ?? 0);

            var query = Models.ModelsFontend.UserWebModel.GetListOrderByCustomerID(cus.CustomerID, status, startDate, endDate, GetSiteGuid()).AsQueryable();
            int totalRows = query.Count();

            int totalPages = (int)Math.Ceiling((double)totalRows / (double)pageSize);
            if (totalPages != 1)
            {
                //paging
                query = query.Skip(((int)currentPage - 1) * (int)pageSize)
                                .Take((int)pageSize);
            }

            UpdatePagerViewData(totalPages, totalRows, (int)currentPage, (int)pageSize, null);

            var result = query.ToList();

            if (query.Count() > 0)
            {

            }

            ViewData["idList"] = id;

            if (totalRows == 0)
                return PartialView("../BaseFontend/" + GetSiteGuid() + "/HistoryOrder", new List<SCSService.Customer_Order>());
            else
                return PartialView("../BaseFontend/" + GetSiteGuid() + "/HistoryOrder", result);
        }

        public ActionResult HistoryTransactionDetail(int? id)
        {
            IntPage();
            if (Session["UserID"] == null)
            {
                if (DefaultHostGetter().Contains("localhost"))
                {
                    return Redirect("/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }
                else
                {
                    return Redirect("/" + DefaultLanguageGetter() + "/UserWeb/SignIn");
                }


            }

            if (DefaultHostGetter().Contains("localhost"))
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultSiteGuidGetter() + "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }
            else
            {
                olUrl = Request.UrlReferrer.AbsoluteUri != null ? Request.UrlReferrer.AbsoluteUri : "/" + DefaultLanguageGetter() + "/UserWeb/SignIn";
            }


            return View();
        }


        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult HistoryOrderDetail(int? id)
        {
            //id is orderID
            int userID = int.Parse(Session["UserID"].ToString());
            int currentPage = 1;
            int pageSize = 9;

            ViewData["currentPage"] = currentPage;
            ViewData["pageSize"] = pageSize;

            return HistoryOrderDetail(currentPage, pageSize, null, id);
        }

        [ChildActionOnly]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult HistoryOrderDetail(int? currentPage, int? pageSize, string gridAction, int? id)
        {
            int LanguageID = StockModel.GetLanguageIDByCountryCode(DefaultLanguageGetter());
            int siteGuid = GetSiteGuid();


            //id đầu vào là orderID
            if (gridAction == "PageSizeChanged")
                currentPage = 1;

            if (currentPage == null && gridAction == "CurrentPageChanged")
            {
                string strCurrentPage = Request.Form.Get("quickPage").Replace(",", "");
                currentPage = int.Parse(strCurrentPage);
            }
            else if (currentPage == null)
            {
                currentPage = 1;
            }

            if (pageSize == null)
            {
                pageSize = 9;
            }

            ViewData["currentPage"] = currentPage;

            var cus = new SCSService.Customer();
            cus = Models.ModelsFontend.ShoppingCart.GetCustomerByUserID(id ?? 0);

            var query = Models.ModelsFontend.UserWebModel.CustomerOrderDetail(id).AsQueryable();
            int totalRows = query.Count();

            int totalPages = (int)Math.Ceiling((double)totalRows / (double)pageSize);
            if (totalPages != 1)
            {
                //paging
                query = query.Skip(((int)currentPage - 1) * (int)pageSize)
                                .Take((int)pageSize);
            }

            UpdatePagerViewData(totalPages, totalRows, (int)currentPage, (int)pageSize, null);

            var result = query.ToList();

            if (query.Count() > 0)
            {

            }

            ViewData["idList"] = id;

            if (totalRows == 0)
                return PartialView("../BaseFontend/" + GetSiteGuid() + "/HistoryOrderDetail", new List<SCSService.Detail_Order>());
            else
                return PartialView("../BaseFontend/" + GetSiteGuid() + "/HistoryOrderDetail", result);
        }
    }
}
